import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.InputStream;

@WebServlet(urlPatterns = {"/upload"})
@MultipartConfig(location = "/home/kkk/Documents/trash/JavaEE/experiment_1/")// location改为本地的绝对路径
public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
	

        // 根据@MultipartConfig的location配置的目录上传文件，直接传参文件名即可。
	DatabaseConnect db =new DatabaseConnect(getServletContext());
	String username = req.getSession().getAttribute("username").toString();
	InputStream inputstream = req.getPart("portrait").getInputStream();
	db.updateportrait(username,inputstream);


	    if(db.getimage(username) != null)
		req.getSession().setAttribute("portrait", db.getimage(username));
	try {
	    System.out.println("about to dispatch");
	    req.getRequestDispatcher("profile.jsp").forward(req, res);
	} catch (ServletException e) {
	    e.printStackTrace();
	}
    }
}
